package jfox.platform.organization.dao;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.QueryHint;
import jfox.platform.organization.entity.Department;
import jfox.platform.infrastructure.EntityObject;
import jfox.platform.infrastructure.SuperDAO;

/**
 * @author <a href="mailto:sheshengdian@gmail.com">sheshengdian</a>
 */
@Stateless
@Local
@NamedNativeQueries(
        {
        @NamedNativeQuery(
                name = DepartmentDAOBean.GET_DEPARTMENT_BY_ID,
                query = "SELECT * FROM T_SYS_DEPARTMENT WHERE ID=$ID AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
                resultClass = Department.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
        @NamedNativeQuery(
                name = DepartmentDAOBean.GET_DEPARTMENT_BY_COMPANYID,
                query = "SELECT * FROM T_SYS_DEPARTMENT WHERE COMPANY_ID=$COMPANY_ID AND VSTATUS=" + EntityObject.VSTATUS_NORMAL,
                resultClass = Department.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),

        @NamedNativeQuery(
                name = DepartmentDAOBean.GET_ALL_DEPARTMENTS,
                query = "SELECT * FROM T_SYS_DEPARTMENT WHERE VSTATUS=" + EntityObject.VSTATUS_NORMAL + " ORDER BY ID",
                resultClass = Department.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }
        ),
    
      
        @NamedNativeQuery(
                name = DepartmentDAOBean.INSERT_DEPARTMENT,
                query = "INSERT INTO T_SYS_DEPARTMENT(" +
                        "ID," +
                        "NAME, " +
                        "PRIORITY, " +
                        "VSTATUS, " +
                        "MANAGER, " +
                        "MANAGER_PHONE," +
                        "MANAGER_EMAIL," +
                        "COMPANY_ID) " +
                        "VALUES (" +
                        "$DEPARTMENT.getId()," +
                        "$DEPARTMENT.getName()," +
                       // "$DEPARTMENT.getLocalName()," +
                        "$DEPARTMENT.getPriority()," +
                       // "$DEPARTMENT.getCreateTime()," +
                       // "$DEPARTMENT.getCreator()," +
                       // "$DEPARTMENT.getLastModified()," +
                      //  "$DEPARTMENT.getLastModifier()," +
                        "$DEPARTMENT.getVstatus()," +
                       // "$DEPARTMENT.getDescription()," +
                        "$DEPARTMENT.getManager()," +
                        "$DEPARTMENT.getManagerPhone()," +
                        "$DEPARTMENT.getManagerEmail()," +
                        "$DEPARTMENT.getCompanyId())",
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        ),
        @NamedNativeQuery(
                name = DepartmentDAOBean.DELETE_DEPARTMENT,
                query = "UPDATE T_SYS_DEPARTMENT SET VSTATUS = " + EntityObject.VSTATUS_REMOVED + " WHERE COMPANY_ID=$COMPANY_ID",
                resultClass = Department.class,
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        ),
        @NamedNativeQuery(
                name = DepartmentDAOBean.UPDATE_DEPARTMENT,
                query = "UPDATE T_SYS_DEPARTMENT" +
                        " SET NAME = " + "$DEPARTMENT.getName()," +
                        //" LOCAL_NAME = " + "$DEPARTMENT.getLocalName()," +
                        " PRIORITY = " + "$DEPARTMENT.getPriority()," +
                       // " CREATE_TIME = " + "$DEPARTMENT.getCreateTime()," +
                       // " CREATOR = " + "$DEPARTMENT.getCreator()," +
                       // " LAST_MODIFIED = " + "$DEPARTMENT.getLastModified()," +
                       // " LAST_MODIFIER = " + "$DEPARTMENT.getLastModifier()," +
                        " VSTATUS = " + "$DEPARTMENT.getVstatus()," +
                       // " DESCRIPTION = " + "$DEPARTMENT.getDescription()," +
                        " MANAGER = " + "$DEPARTMENT.getManager()," +
                        " MANAGER_PHONE = " + "$DEPARTMENT.getManagerPhone()," +
                        " MANAGER_EMAIL = " + "$DEPARTMENT.getManagerEmail()," +
                        " COMPANY_ID = " + "$DEPARTMENT.getCompanyId()" +
                       
                        " WHERE ID=" + "$DEPARTMENT.getId()",
                hints = {
                @QueryHint(name = "cache.partition", value = "function")
                        }

        )

                }
)

public class DepartmentDAOBean extends SuperDAO implements DepartmentDAO {
    public final static String GET_DEPARTMENT_BY_ID = "getDepartmentById";
    public final static String GET_ALL_DEPARTMENTS = "getAllDepartments";
    public final static String INSERT_DEPARTMENT = "insertDepartment";
    public final static String DELETE_DEPARTMENT = "deleteDepartment";
    public final static String UPDATE_DEPARTMENT = "updateDepartment";
    public final static String GET_DEPARTMENT_BY_COMPANYID= "getDepartmentByCompany";


    public void insertDepartment(Department department) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("DEPARTMENT", department);
        executeNamedNativeUpdate(INSERT_DEPARTMENT, params);
    }

    public int deleteDepartment(long id) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("COMPANY_ID", id);
        return executeNamedNativeUpdate(DepartmentDAOBean.DELETE_DEPARTMENT, params);
    }

    public int updateDepartment(Department department) {
        Map<String, Object> params = new HashMap<String, Object>(1);
        params.put("DEPARTMENT", department);
        return executeNamedNativeUpdate(UPDATE_DEPARTMENT, params);
    }

    public Department getDepartmentById(long id) {
        return (Department)getEntityObjectByColumn(DepartmentDAOBean.GET_DEPARTMENT_BY_ID, "ID", id);
    }
    
    public Department getDepartmentByCompany(long companyId) {
        return (Department)getEntityObjectByColumn(DepartmentDAOBean.GET_DEPARTMENT_BY_COMPANYID, "COMPANY_ID", companyId);
    }

    public List<Department> getAllDepartments() {
        return (List<Department>)processNamedNativeQuery(DepartmentDAOBean.GET_ALL_DEPARTMENTS, Collections.EMPTY_MAP);
    }



}
